/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author QuangHau
 */
public class DataProvider {
    public static String connectionString = "";
    
    public static String getConnectionString() {
         if(connectionString.equals("")) {
            return ReadConnectionString();
         }
         return connectionString;
    }
    
    public static String ReadConnectionString(){
            File f = new File ("Database.txt");
            if(!f.exists()) {
                try (FileWriter fw = new FileWriter("Database.txt")){
                    String str = "localhost\r\n";// host
                    fw.write(str);
                    
                    str = "1433\r\n";// port
                    fw.write(str);
                    
                    str = "JavaDB\r\n";// Database
                    fw.write(str);
                    
                    str = "sa\r\n";// user
                    fw.write(str);
                    
                    str = "123456\r\n";// password
                    fw.write(str);
       
                    fw.close();
                    connectionString = "jdbc:sqlserver://localhost:1433;databaseName=JavaDB;user=sa;password=123456";
                } catch (IOException ex) {
                    Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
                } 
            }
            else{
                
                try (BufferedReader br = new BufferedReader(new FileReader("Database.txt"))) {
                    String str = "jdbc:sqlserver://";                   
                    str += br.readLine();// host
                    str += ":" + br.readLine();// port
                    str += ";databaseName=" + br.readLine();// Database
                    str += ";user=" + br.readLine();// user
                    str += ";password=" + br.readLine();
                    br.close();
                    connectionString = str;
                } catch (IOException ex) {
                    Logger.getLogger(DataProvider.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            //connectionString = "jdbc:sqlserver://localhost:1433;databaseName=Hocsinh;user=sa;password=123456";
            return connectionString;
    }  
    
    public static Connection Connection()
    {
        Connection con = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   
            con = DriverManager.getConnection(getConnectionString());
            return con;
        } catch (ClassNotFoundException | SQLException ex) {
        }
        return con;
    }
    
    public static boolean Successful()
    {
        Connection con = null;
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");   
            con = DriverManager.getConnection(getConnectionString());
            if(con != null){
                con.close();
                return true;
            }
        } catch (ClassNotFoundException | SQLException ex) {
            return false;
        }
        return false;
    }
}
